<template>
  <div class='login_conn'>
    <div class="comm_height comm_flex">

      <div class="comm_height comm_flex loge_main">
      </div>

      <div style="width: 70%;display: flex">
        <div style="width: 70%"></div>
        <div style="width: 30%;display: flex;align-items: center;">
          <div style="padding: 10px ;display: flex;align-items: center;" @click="switchLanguage('zh_CN')" class="xvant">
            <img src="../assets/image/not_selected.png" alt="" width="20" height="20">
            <span :class="language==='zh_CN'?'not_cted':'cted'" style="padding: 10px">简体中文</span>
          </div>
          <div style="display: flex;align-items: center;" @click="switchLanguage('en_US')" class="xvant">
            <img src="../assets/image/not_selected.png" alt="" width="20" height="20">
            <span :class="language==='en_US'?'not_cted':'cted'" style="padding: 10px">English</span>
          </div>
        </div>
      </div>

    </div>
    <div class='login_box'>
      <div class='img_box'>
        <img src='../assets/sunpx_wl.png' alt/>
      </div>
      <el-form class='form_login' :rules='loginRules' :model='user' ref='loginForm'>
        <el-form-item prop='username'>
          <el-input prefix-icon='el-icon-user-solid' v-model='user.username'
                    :placeholder="$t('home.username')"></el-input>
        </el-form-item>

        <el-form-item prop='password'>
          <el-input prefix-icon='el-icon-unlock' v-model='user.password' show-password :placeholder="$t('home.password')">></el-input>
        </el-form-item>

        <el-form-item class='btns'>
          <el-button type='primary' @click='loginConfirmation'>{{$t("home.login")}}</el-button>
          <el-button type='info' @click='LoginReset'>{{$t("home.reset")}}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <slideAss :dialogVisible.sync="dialogVisible" @login="login"/>
  </div>
</template>
<script>
import {initRouter} from '../router/index'
import {login} from '../api/api'
import {mapState} from 'vuex'
import slideAss from './slide/slideAssembly.vue'
export default {
  components:{slideAss},
  data() {
    return {
      user: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          {required: true, message: this.$t('home.username'), trigger: 'blur'},
          { min: 5, max: 15, message: this.$t('home.hint'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('home.password'), trigger: 'blur' },
          { min: 5, max: 15, message: this.$t('home.hint'), trigger: 'blur' }
        ]
      },
      dialogVisible:false
    }
  },
  methods: {
    //   重置方面
    LoginReset() {
      this.user = {}
      this.$refs.loginForm.resetFields()
    },
    loginConfirmation(){
      this.$refs.loginForm.validate(async valid => {
          if (!valid) return
          this.dialogVisible=true
      })
    },
    // 登录
    login() {
      this.$refs.loginForm.validate(async valid => {
          if (!valid) return
        console.log(this.user)
        login(this.user).then(res => {
          ////console.log(res)
          window.sessionStorage.setItem('sign', res.data.sign)
          this.$store.commit('setmodueList', res.data.modul)
          this.$store.commit('setBtus', res.data.btu)
          this.$store.commit('setUserInfo', res.data.userInfo)
          initRouter()
          this.$notify({title: '成功', message: '登录成功', type: 'success'});

          this.$router.push('/home')

          })
          }
      )


    },
    setUpLanguage() {
      if (this.language === null || this.language === undefined) {
        this.$store.commit("setLanguage", this.$i18n.locale)
        return
      }
      this.$i18n.locale = this.language
    },
    switchLanguage(rew) {
      this.$store.commit("setLanguage", rew)
      this.$i18n.locale = rew
    }
  },
  computed: {
    ...mapState(['language'])
  },
  created() {
    this.setUpLanguage()
  }
}
</script>

<style lang='less' scoped>
.login_conn {
  //background-image: url("../assets/ba.jpg");
  background-color: #1D1C2E;
  height: 100%;
  background-size: 100% 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .img_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #dddddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;

    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.form_login {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}


.comm_height {
  height: 100px;
}

.comm_flex {
  display: flex
}

.loge_main {
  width: 30%;
  margin-left: 20px;
  align-items: center;
  background: #1D1C2E;

  img {
    display: block;
    margin: 10px;
  }
}

.not_cted {
  color: #005aff;
  font-size: 15px;
}

.cted {
  color: #EEEEEE;
  font-size: 15px;
}

.xvant :hover {
  text-decoration: underline;
}
</style>
